package domain;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class Bcard {

	// 卡号
	private String bCardId;
	// 密码
	private String bPwd;
	// 余额
	private double bBalance;
	// 水电
	private double bPhone;
	// 话费
	private double bWater;

	private static Bcard bcard;

	// 使用单列模式
	public static Bcard bcard() {
		if (bcard == null) {
			bcard = new Bcard();
		}
		return bcard;
	}

	private Bcard() {

	}

	/**
	 * 
	 * @Title: editBacrd @author Huang_shengjun @Description:
	 * 修改操作update @param @param cardid @param @param balance @param @throws
	 * Exception 参数 @return void 返回类型 @date 2020年5月19日 下午8:30:11 @throws
	 */
	public void editBacrd(String cardid, double balance) throws Exception {
		Connection conn = null;
		try {
			bcard.bCardId = cardid;
			bcard.bBalance = balance;
			System.out.println("==editBacrd-->" + bcard.bCardId + ";" + bcard.bBalance);
			conn = DbUtil.getConn();
			// 开启手动提交事务
			conn.setAutoCommit(false);
			// 保存到数据库
			PreparedStatement pstmt = null;
			try {
				StringBuffer sqlBuff = new StringBuffer("update t_card set b_balance=? where bcard_id=?");
				pstmt = conn.prepareStatement(sqlBuff.toString());
				pstmt.setDouble(1, bcard.bBalance);
				pstmt.setString(2, bcard.bCardId);
				pstmt.executeUpdate();
			} catch (SQLException e) {
				throw e;
			} finally {
				System.out.println("==执行结束，editBacrd数据连接-->close");
				DbUtil.close(pstmt);
			}
			// 提交事务
			conn.commit();
		} catch (Exception e) {
			// 事务回滚
			if (conn != null) {
				conn.rollback();
			}
			throw e;
		} finally {
			DbUtil.close(conn);
		}
	}

	/**
	 * 
	 * @Title: login @author Huang_shengjun @Description:
	 * 登录验证，得到账户密码、余额 @param @param cardid @param @param pwd 参数 @return void
	 * 返回类型 @date 2020年5月19日 下午8:31:35 @throws
	 */
	public String login(String cardid, String pwd) {

		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			conn = DbUtil.getConn();
			System.out.println("==login-->" + cardid + ";" + pwd);
			pstmt = conn.prepareStatement("select * from t_card where bcard_id=? and b_pwd=? ");
			int index = 1;
			pstmt.setString(index++, cardid);
			pstmt.setString(index, pwd);
			rs = pstmt.executeQuery();
			if (rs != null && rs.next()) {
				bcard.bCardId = rs.getString("bcard_id");
				bcard.bPwd = rs.getString("b_pwd");
				bcard.bBalance = rs.getDouble("b_balance");
				bcard.bPhone = rs.getDouble("b_phone");
				bcard.bWater = rs.getDouble("b_water");
			} else {
				System.out.println(" with id :" + cardid + " could not be loaded from the database.无法从数据库加载");
				System.out.println("登录失败");
				return null;
			}
		} catch (SQLException e) {
			System.out.println(" with id :" + cardid + " could not be loaded from the database.无法从数据库加载");
		} finally {
			System.out.println("==登录执行之后bBalance-->" + bcard.bCardId + ";" + bcard.bPwd + ";" + bcard.bBalance);

			DbUtil.close(rs, pstmt, conn);
		}
		System.out.println("登录成功");
		return "success";
	}

	/**
	 * 取款
	 * 
	 * @param amount
	 * @return 0:成功 1:不成功
	 */
	public int withdraw(double amount) {
		System.out.println("==bBalance-->" + bcard.bBalance + "；amount-->" + amount);
		if (this.bBalance >= amount) {
			System.out.println("==withdraw取款前-->" + bcard.bBalance);
			bcard.bBalance = bcard.bBalance - amount;
			try {
				editBacrd(bcard.bCardId, bcard.bBalance);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return 1;// 失败返回1
			}
			System.out.println("==withdraw取款后-->" + bcard.bBalance);
			return 0; // 成功返回0
		}
		return 1;// 失败返回1
	}

	/**
	 * 存款
	 * 
	 * @param amount
	 */
	public int deposit(double amount) {
		bcard.bBalance += amount;
		try {
			editBacrd(bcard.bCardId, bcard.bBalance);
			System.out.println("deposit存款后-->" + bcard.bBalance);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return 1;// 失败返回
		}
		return 0; // 成功返回0
	}

	/**
	 * 
	 * @Title: isCard @author Huang_shengjun @Description: 卡号验证 @param @param
	 * cardid @param @return 参数 @return String 返回类型 @date 2020年5月29日
	 * 下午3:33:21 @throws
	 */
	public String isCard(String cardid) {

		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			conn = DbUtil.getConn();
			pstmt = conn.prepareStatement("select * from t_card where bcard_id=? and is_lock = 0 ");
			int index = 1;
			pstmt.setString(index++, cardid);
			rs = pstmt.executeQuery();
			if (rs != null && rs.next()) {
				System.out.println("==验证卡号成功");
			} else {
				System.out.println(" with id :" + cardid + " could not be loaded from the database.无法从数据库加载");
				return null;
			}
		} catch (SQLException e) {
			System.out.println(" with id :" + cardid + " could not be loaded from the database.无法从数据库加载");
			return null;
		} finally {
			DbUtil.close(rs, pstmt, conn);
		}
		return "success";
	}

	/**
	 * 
	 * @Title: howMoney @author Huang_shengjun @Description: 根据卡号查询余额 @param @param
	 * cardid @param @return 参数 @return Double 返回类型 @date 2020年5月29日
	 * 下午4:59:41 @throws
	 */
	public Double howMoney(String cardid) {
		Double thisMoney = 0.0;
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			conn = DbUtil.getConn();
			pstmt = conn.prepareStatement("select * from t_card where bcard_id=?");
			int index = 1;
			pstmt.setString(index++, cardid);
			rs = pstmt.executeQuery();
			if (rs != null && rs.next()) {
				thisMoney = rs.getDouble("b_balance");
				System.out.println("==根据卡号查询余额-->" + thisMoney);
			} else {
				System.out.println(" with id :" + cardid + " could not be loaded from the database.无法从数据库加载");
				return null;
			}
		} catch (SQLException e) {
			System.out.println(" with id :" + cardid + " could not be loaded from the database.无法从数据库加载");
			return null;
		} finally {
			DbUtil.close(rs, pstmt, conn);
		}
		return thisMoney;
	}

	/**
	 * 
	 * @Title: transferMoney @author Huang_shengjun @Description:转账操作 @param @param
	 * cardtwo @param @param money @param @return 参数 @return String 返回类型 @date
	 * 2020年5月29日 下午5:44:35 @throws
	 */
	public String transferMoney(String cardtwo, Double money) {
		String cardOne = bcard.bCardId;
		// 先获取两个卡号里面的钱
		Double moneyone = howMoney(cardOne);
		Double moneytwo = howMoney(cardtwo);
		System.out.println("==查询余额后moneyone-->" + moneyone + " ;moneytwo-->" + moneytwo);
		moneyone -= money;
		moneytwo += money;
		System.out.println("==查询余额减加钱后moneyone-->" + moneyone + " ;moneytwo-->" + moneytwo);
		try {
			// 转账
			editBacrd(cardtwo, moneytwo);
			// 扣钱
			editBacrd(cardOne, moneyone);
			// 以上顺序不能反，修改操作后会赋值新的卡号
			System.out.println("==转账后-->" + bcard.bBalance);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;// 失败返回
		}
		return "success"; // 成功返回
	}

	public void editPwd(int pwd) throws Exception {
		String cardOne = bcard.bCardId;
		Connection conn = null;
		try {
			conn = DbUtil.getConn();
			// 开启手动提交事务
			conn.setAutoCommit(false);
			// 保存到数据库
			PreparedStatement pstmt = null;
			try {
				StringBuffer sqlBuff = new StringBuffer("update t_card set b_pwd=? where bcard_id=?");
				pstmt = conn.prepareStatement(sqlBuff.toString());
				pstmt.setInt(1, pwd);
				pstmt.setString(2, cardOne);
				pstmt.executeUpdate();
			} catch (SQLException e) {
				throw e;
			} finally {
				System.out.println("==执行结束，editBacrd数据连接-->close");
				DbUtil.close(pstmt);
			}
			// 提交事务
			conn.commit();
		} catch (Exception e) {
			// 事务回滚
			if (conn != null) {
				conn.rollback();
			}
			throw e;
		} finally {
			DbUtil.close(conn);
		}
	}

	/**
	 * 
	 * @Title: pwdLock @author Huang_shengjun @Description: 密码错误三次，锁卡 @param @param
	 * cardid @param @throws Exception 参数 @return void 返回类型 @date 2020年5月30日
	 * 上午10:59:45 @throws
	 */
	public void pwdLock(String cardid) throws Exception {
		Connection conn = null;
		try {
			System.out.println("==pwdLock锁卡-->" + cardid);
			conn = DbUtil.getConn();
			// 开启手动提交事务
			conn.setAutoCommit(false);
			// 保存到数据库
			PreparedStatement pstmt = null;
			try {
				StringBuffer sqlBuff = new StringBuffer("update t_card set is_lock =1 where bcard_id=?");
				pstmt = conn.prepareStatement(sqlBuff.toString());
				pstmt.setString(1, cardid);
				pstmt.executeUpdate();
			} catch (SQLException e) {
				throw e;
			} finally {
				System.out.println("==执行结束，pwdLock锁卡数据连接-->close");
				DbUtil.close(pstmt);
			}
			// 提交事务
			conn.commit();
		} catch (Exception e) {
			// 事务回滚
			if (conn != null) {
				conn.rollback();
			}
			throw e;
		} finally {
			DbUtil.close(conn);
		}
	}

	/**
	 * 
	 * @Title: editPayment @author Huang_shengjun @Description: 代缴费扣取 @param @param
	 * i @param @param balance @param @throws Exception 参数 @return void 返回类型 @date
	 * 2020年5月30日 下午1:11:41 @throws
	 */
	public void editPayment(int i, double balance) throws Exception {
		Connection conn = null;
		try {
			conn = DbUtil.getConn();
			// 开启手动提交事务
			conn.setAutoCommit(false);
			// 保存到数据库
			PreparedStatement pstmt = null;
			try {
				if (i == 0) {
					System.out.println("==执行水费DB==");
					bcard.bBalance = bcard.bBalance - balance;
					bcard.bWater = bcard.bWater + balance;
					// 水费
					StringBuffer sqlBuff = new StringBuffer(
							"update t_card set b_balance=? ,b_water=? where bcard_id=?");
					pstmt = conn.prepareStatement(sqlBuff.toString());
					pstmt.setDouble(1, bcard.bBalance);
					pstmt.setDouble(2, bcard.bWater);
					pstmt.setString(3, bcard.bCardId);
					pstmt.executeUpdate();
				} else if (i == 1) {
					System.out.println("==执行话费DB==");
					bcard.bBalance = bcard.bBalance - balance;
					bcard.bPhone = bcard.bPhone + balance;
					// 话费
					StringBuffer sqlBuff = new StringBuffer(
							"update t_card set b_balance=? ,b_phone=? where bcard_id=?");
					pstmt = conn.prepareStatement(sqlBuff.toString());
					pstmt.setDouble(1, bcard.bBalance);
					pstmt.setDouble(2, bcard.bPhone);
					pstmt.setString(3, bcard.bCardId);
					pstmt.executeUpdate();
				}

			} catch (SQLException e) {
				throw e;
			} finally {
				System.out.println("==执行结束，editBacrd数据连接-->close");
				DbUtil.close(pstmt);
			}
			// 提交事务
			conn.commit();
		} catch (Exception e) {
			// 事务回滚
			if (conn != null) {
				conn.rollback();
			}
			throw e;
		} finally {
			DbUtil.close(conn);
		}
	}

	public String getbCardId() {
		return bCardId;
	}

	public void setbCardId(String bCardId) {
		this.bCardId = bCardId;
	}

	public String getbPwd() {
		return bPwd;
	}

	public void setbPwd(String bPwd) {
		this.bPwd = bPwd;
	}

	public double getbBalance() {
		return bBalance;
	}

	public void setbBalance(double bBalance) {
		this.bBalance = bBalance;
	}

	public double getbPhone() {
		return bPhone;
	}

	public void setbPhone(double bPhone) {
		this.bPhone = bPhone;
	}

	public double getbWater() {
		return bWater;
	}

	public void setbWater(double bWater) {
		this.bWater = bWater;
	}

}
